package cn.iocoder.yudao.module.cmt.controller.admin.person;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cmt.controller.admin.person.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.person.PersonInfoDO;
import cn.iocoder.yudao.module.cmt.service.person.PersonInfoService;

@Tag(name = "管理后台 - 人员信息")
@RestController
@RequestMapping("/cmt/person-info")
@Validated
public class PersonInfoController {

    @Resource
    private PersonInfoService personInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建人员信息")
    @PreAuthorize("@ss.hasPermission('cmt:person-info:create')")
    public CommonResult<Long> createPersonInfo(@Valid @RequestBody PersonInfoSaveReqVO createReqVO) {
        return success(personInfoService.createPersonInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新人员信息")
    @PreAuthorize("@ss.hasPermission('cmt:person-info:update')")
    public CommonResult<Boolean> updatePersonInfo(@Valid @RequestBody PersonInfoSaveReqVO updateReqVO) {
        personInfoService.updatePersonInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除人员信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:person-info:delete')")
    public CommonResult<Boolean> deletePersonInfo(@RequestParam("id") Long id) {
        personInfoService.deletePersonInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除人员信息")
    @PreAuthorize("@ss.hasPermission('cmt:person-info:delete')")
    public CommonResult<Boolean> deletePersonInfoList(@RequestParam("ids") List<Long> ids) {
        personInfoService.deletePersonInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得人员信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:person-info:query')")
    public CommonResult<PersonInfoRespVO> getPersonInfo(@RequestParam("id") Long id) {
        PersonInfoDO personInfo = personInfoService.getPersonInfo(id);
        return success(BeanUtils.toBean(personInfo, PersonInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得人员信息分页")
    @PreAuthorize("@ss.hasPermission('cmt:person-info:query')")
    public CommonResult<PageResult<PersonInfoRespVO>> getPersonInfoPage(@Valid PersonInfoPageReqVO pageReqVO) {
        PageResult<PersonInfoDO> pageResult = personInfoService.getPersonInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PersonInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出人员信息 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:person-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPersonInfoExcel(@Valid PersonInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PersonInfoDO> list = personInfoService.getPersonInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "人员信息.xls", "数据", PersonInfoRespVO.class,
                        BeanUtils.toBean(list, PersonInfoRespVO.class));
    }

}
